{-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings #-}
module Handler.MasterCatalog.Demo where

import Foundation

import Settings.StaticFiles

import Text.Printf

import Control.Applicative ( (<$>) )
import Data.Text.Read (rational)
import Data.Aeson

import Handler.Types

import Handler.MasterCatalog.QueryGuide ( getGuideInfo )

import qualified Handler.Common as C
import Handler.Common hiding (header)

getMCMapR :: Instrument -> Ra -> Dec -> Zoom -> Handler RepHtml
getMCMapR inst ra dec zoom =  do

  -- получение угла повората телескопа и списка звезд в полях зредния ДГ.
  mgi <- getGuideInfo inst (3,15) ra dec

  (rot, gs) <- case mgi of
      Nothing  -> return (0,[])
      Just  gi -> return gi

  let
    gStarArray = toJSON $ (\(Star ra dec mag) -> object ["ra" .= ra, "dec" .= dec, "mag" .= mag]) <$> gs

    mapId  = "map" :: String

    angl   = printf "%.3f" rot :: String 
    
    left = [whamlet|
      <div .mscMap ##{mapId}>
    |]

--      <p .summary .p>
--         <b>Мастер Каталог

    right = [whamlet|
      <p .section_title .p>
          ЗАДАТЬ ЦЕНТР КАРТЫ
      <p .section .p>
         <lable .label>Ra (&alpha;):
         <input .lonTextBox .textbox type="text" value=#{ra}>
      <p .section .p>
         <lable .label>Dec (&delta;):
         <input .latTextBox .textbox type="text" value=#{dec}>
      <p .section .p>
         <input .settingButton   .button type="button" value="Применить">
         &nbsp;
         <input .centeringButton .button type="button" value="Центровать">

      <p .section_title .p>
          ИНФОРМАЦИЯ
      <p .section .p>
         <span .mapLink>
         <lable .label>Угол разворота:
         <span>&phi; = #{ angl }&deg;
         <lable .label>Коор. центра карты:
         <span .coordMapCenter>
         <lable .label>О звезде:
         <span .objInfo>
    |]

  defaultLayout $ do
    addStylesheet $ StaticR css_mcstyle_css
    
    addScript $ StaticR js_jquery_js
    addScript $ StaticR js_proj4js_js

    addScript $ StaticR js_OpenLayers_js
    addScript $ StaticR js_BBOX_js
    
    addJulius $(juliusFile "mcinit")
    addWidget $(widgetFile "mcmap")

getMCDemoLinkR :: Instrument -> Ra -> Dec -> Zoom -> Handler RepHtml
getMCDemoLinkR inst ra dec zoom =  do

  demoWidget inst ra dec zoom

getMCDemoR :: Handler RepHtml
getMCDemoR = do

  ra   <- maybe (0::Ra)   (either (const 0) fst . rational) <$> lookupGetParam "ra"
  dec  <- maybe (0::Dec)  (either (const 0) fst . rational) <$> lookupGetParam "dec"
  zoom <- maybe (0::Zoom) (either (const 0) fst . rational) <$> lookupGetParam "zoom"

  demoWidget ISSIS ra dec zoom

demoWidget :: Instrument -> Ra -> Dec -> Zoom -> Handler RepHtml
demoWidget inst ra dec zoom =  do

  defaultLayout $ do
    setTitle "Master Catalog Demonstration"
    
    addStylesheet $ StaticR css_mcstyle_css
    
    addWidget $(widgetFile "mcdemo2")

   where
    header = do 
      C.header
      [whamlet|
          <div .grid_10 .alpha .omega .prefix_1 .suffix_1 .panel>
             <div .headline .grid .panel>
                 ДЕМОНСТРАЦИЯ
      |]

-----------------------------------------------------------------------------------------

{--

getMCDemoR_ :: Handler RepHtml
getMCDemoR_ = do

  ra   <- maybe (0::Ra)   (either (const 0) fst . rational) <$> lookupGetParam "ra"
  dec  <- maybe (0::Dec)  (either (const 0) fst . rational) <$> lookupGetParam "dec"
  rot  <- maybe (0::Deg)  (either (const 0) fst . rational) <$> lookupGetParam "rot"
  zoom <- maybe (0::Zoom) (either (const 0) fst . rational) <$> lookupGetParam "zoom"

  v <- getStars ISSIS (3,15) ra dec

  (rot, xs)<- case v of
      Nothing -> return (rot,[])
      Just (angl, xs, _) -> return (angl*180/pi, xs)

  let
    gs = toJSON $ map (\x -> object ["ra" .= ra5 x, "dec" .= dec5 x, "mag" .= mag5 x]) xs

    mapId = "map" :: String

    angl  = printf "%.3f" rot :: String 
    
    header = do 
      C.header
      [whamlet|
          <div .grid_10 .alpha .omega .prefix_1 .suffix_1 .panel>
             <div .headline .grid .panel>
                 ДЕМОНСТРАЦИЯ
      |]

    left = [whamlet|
      <div .mscMap ##{mapId}>
    |]

    right = [whamlet|
      <p .summary .p>
         <b>Мастер Каталог

      <p .section_title .p>
          ЗАДАТЬ ЦЕНТР КАРТЫ
      <p .section .p>
         <lable .label>Ra (&alpha;):
         <input .lonTextBox .textbox type="text" value=#{ra}>
      <p .section .p>
         <lable .label>Dec (&delta;):
         <input .latTextBox .textbox type="text" value=#{dec}>
      <p .section .p>
         <input .angleTextBox .textbox .disabled type="hidden" value=#{rot}>
      <p .section .p>
         <input .continueButton .button type="button" value="Применить">

      <p .section_title .p>
          ИНФОРМАЦИЯ
       <p .section .p>
         <lable .label>Угол разворота:
         <span>&phi; = #{ angl }&deg;
         <lable .label>Коор. центра карты:
         <span .coordMapCenter>
      <p .section .p>
         <lable .label>О звезде:
         <span .objInfo>
    |]

  defaultLayout $ do
    setTitle "Master Catalog Demonstration"
    
    addStylesheet $ StaticR css_mcstyle_css
    
    addScript $ StaticR js_jquery_js
    addScript $ StaticR js_proj4js_js

    addScript $ StaticR js_OpenLayers_js
    addScript $ StaticR js_BBOX_js
    
    addJulius $(juliusFile "mcinit")
    addWidget $(widgetFile "mcdemo")

--}